unit clsBanco;

interface

uses Dm, dbtables, biblio;

type
  TBanco = class(TObject)
  private
    Ban_codigo: string;
  public
    Ban_Nome: string;
    Ban_fantasia: string;
    function Gravar(p_Ban_codigo: string): Byte;
    function Recuperar(p_Ban_codigo: string): Byte;
    function Recuperar_Nome(p_Ban_nome: string): string;
    function Excluir(p_Ban_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TBanco.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Banco where Ban_codigo=:k_Ban_codigo');
  tq.parambyname('k_Ban_codigo').asstring := strzero(p_Ban_codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Banco VALUES(:k_Ban_codigo,:k_Ban_Nome,:k_Ban_fantasia)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Banco SET Ban_Nome=:k_Ban_Nome,Ban_fantasia=:k_Ban_fantasia WHERE Ban_codigo=:k_Ban_codigo');
  end;
  tq.parambyname('k_Ban_codigo').asstring := strzero(p_Ban_codigo, 3);
  tq.parambyname('k_Ban_Nome').asstring := Ban_Nome;
  tq.parambyname('k_Ban_fantasia').asstring := Ban_fantasia;
  tq.close;
  tq.execsql;
  result := 1;
  tq.Free;
end;

function TBanco.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Banco where Ban_codigo=:k_Ban_codigo');
  tq.Params[0].asstring := strzero(p_Ban_codigo, 3);
  tq.open;
  Ban_codigo := p_Ban_codigo;
  if tq.eof then begin
    Ban_Nome := '';
    Ban_fantasia := '';
    Result := 0;
  end else begin
    Ban_Nome := tq.fieldbyname('Ban_Nome').asstring;
    Ban_fantasia := tq.fieldbyname('Ban_fantasia').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TBanco.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Banco WHERE Ban_codigo=:k_Ban_codigo');
  tq.parambyname('k_Ban_codigo').asstring := strzero(p_Ban_codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TBanco.Consultar;
begin
  p_q.close;
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Banco where Ban_Nome>=:k_Ban_Nome order by Ban_Nome');
  p_q.parambyname('k_Ban_Nome').asstring := p_nome;
  p_q.open;
end;

function TBanco.Recuperar_Nome(p_Ban_nome: string): string;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Banco where Ban_nome=:k_Ban_nome');
  tq.Params[0].asstring := (p_Ban_nome);
  tq.open;
  if tq.eof then begin
    Result := '';
  end else begin
    Result := tq.FieldByName('Ban_codigo').AsString;
  end;
  tq.Close;
  tq.Free;
end;

end.
